
import AppLayout from '@/views/Layout/index.vue'
import AppLogin from '@/views/Login/index.vue'
import AppNotFound from '@/views/NotFound/index.vue'

import AppMain from '@/views/Main/index.vue'
import AppBannerList from '@/views/Banner/BannerList/index.vue'
import AppBannerSwiperList from '@/views/Banner/BannerList/components/SwiperMode/index.vue'
import AppBannerTableList from '@/views/Banner/BannerList/components/TableMode/index.vue'

import AppBannerCreate from '@/views/Banner/BannerCreate/index.vue'

import AppGoodProList from '@/views/GoodPro/GoodProList/index.vue'
import AppGoodProSearchList from '@/views/GoodPro/GoodProSearchList/index.vue'

import AppAdminList from '@/views/UserAndAdmin/AppAdminList/index.vue'
import _ from 'lodash'
// 路由表
const routes = [
  {
    path: '/', name: 'home', component: AppLayout,
    redirect: { name: 'main' },
    children: [
      // { path: '', redirect: '/main' },
      { path: 'main', name: 'main', title: '首页', component: AppMain },
      {
        path: 'banner-list', parent: { id: 'banner', title: '轮播图管理' }, name: 'banner-list', title: '轮播图列表', component: AppBannerList,
        redirect: { name: 'banner-list-swiper' },
        children: [
          { path: 'swiper', name: 'banner-list-swiper', component: AppBannerSwiperList, meta: { type: 'swiper' } },
          { path: 'table', name: 'banner-list-table', component: AppBannerTableList, meta: { type: 'table' } },
        ]
      },
      { path: 'banner-create', parent: { id: 'banner', title: '轮播图管理' }, name: 'banner-create', title: '新增轮播图', component: AppBannerCreate },
      { path: 'goodpro-normal-list', parent: { id: 'goodpro', title: '商品管理' }, name: 'goodpro-normal-list', title: '商品展示列表', component: AppGoodProList },
      { path: 'goodpro-search-list', parent: { id: 'goodpro', title: '商品管理' }, name: 'goodpro-search-list', title: '商品筛选列表', component: AppGoodProSearchList },
      { path: 'admin-list', parent: { id: 'user-and-admin', title: '用户及管理员' }, name: 'admin-list', title: '管理员列表', component: AppAdminList },
    ]
  },
  { path: '/login', name: 'login', component: AppLogin },
  { path: '/404', name: 'not-found', component: AppNotFound },
  { path: '/:pathMatch(.*)*', redirect: '/' },
]

// 通过路由表生成菜单列表
const transformRoutesToMenu = (routes) => {
  // 取出对应的路由项
  const items = routes[0]?.children || []
  // 遍历存储, 利用键值对的关系, 存储一级菜单及内部的二级菜单
  // { parent1: { title, to, path }, parent2: { title, path, items: [{ title, to, path }, { title, to, path }] } }
  const menusMap = {}

  items.forEach((item) => {
    // 如果当前路由项没有path或者没有title 就忽略
    if (!item.path || !item.title) return false
    // 如果当前的路由项 没有parent, 说明是一级菜单, 直接存入map
    if (!item.parent) {
      menusMap[item.name] = { title: item.title, name: item.name }
    } else {
      // 如果路由项有parent, 但是还没有存入map
      // 就需要存入一个parent, 并将自己也存入
      if (!menusMap[item.parent.id]) {
        menusMap[item.parent.id] = {
          title: item.parent.title,
          name: item.parent.id,
          items: [{ title: item.title, name: item.name }]
        }
      } else {
        // 如果已经有parent存入map了, 那就在已有的map的parent中存入自己
        menusMap[item.parent.id].items.push({ title: item.title, name: item.name })
      }
    }
  })
  // 将对象转化成数组
  return Object.values(menusMap)
}

export const createAuthorityMenus = (menus, checkedKeys) => {
  const _menus = _.cloneDeep(menus)
  const result = []
  _menus.forEach((menu) => {
    if (checkedKeys.includes(menu.name)) {
      result.push(menu)
    } else if (menu.items) {
      // 没有一级的name, 要判断一下有没有二级的name
      menu.items.forEach((item, i) => {
        if (!checkedKeys.includes(item.name)) {
          menu.items.splice(i, 1)
        }
      })
      if (menu.items.length) {
        result.push(menu)
      }
    }
  })
  return result
}

export const menus = transformRoutesToMenu(routes)

export default routes